#pragma once
#include <sstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <time.h>
#include <fstream>

#include "../db/db_connection.h"
#include "../db_generator/generated.h"
#include "../db_generator/patient_table.h"
#include "../db_generator/payment_table.h"

#include "../db_generator/patient_table.h"
#include "../db_generator/payment_table.h"
#include "../db_generator/visit_table.h"
#include "../db_generator/payment_row.h"
#include "../db_generator/patient_row.h"
#include "../db_generator/visit_row.h"

namespace uhdpbs {
	namespace reports {
		class Receivables{
		private:
			float CalculateTotalPayments(uhdpbs::generated::patient_row& patient, uhdpbs::generated::payment_table& payments);

			float ConvertStringToFloat(std::string val);

			std::string PatientHeader(uhdpbs::generated::patient_row& patient);

			std::string GeneratePayments(uhdpbs::generated::patient_row& patient, uhdpbs::generated::payment_table& payments);
			std::string GenerateVisits(uhdpbs::generated::patient_row& patient, uhdpbs::generated::visit_table& visits);

			std::string GenerateAgedVisits(uhdpbs::generated::patient_row& patient, 
				uhdpbs::generated::visit_table& visits, float fPaid);

			std::string GenerateReportForPatient(uhdpbs::generated::patient_row& patient,
				uhdpbs::generated::payment_table& payments, uhdpbs::generated::visit_table& visits);

			/* If left > right, return -1; if left == right, return 0; if left < right return 1 */
			std::string Receivables::Pad(std::string val, int wide, bool dLeftPad = true);
			std::string Receivables::Pad(float val, int wide, bool dLeftPad = true);
			int CalculateDaysOld(time_t date);
			int CalculateDaysOld(std::string date);
			time_t ConvertToTimeT(std::string val);

			std::string Receivables::PadMoney(std::string val, int wide, bool dLeftPad = true);
			std::string Receivables::PadMoney(float val, int wide, bool dLeftPad = true);

		public:
			Receivables();
			std::string Generate(uhdpbs::generated::patient_table& patients);

			static void AgedReceivablesMenu(uhdpbs::db::connection& dbConn);
			static void SinglePatientReport(uhdpbs::db::connection& dbConn);
			static void AllPatientsReport(uhdpbs::db::connection& dbConn);
			static void SinglePatientPrintOut(uhdpbs::db::connection& dbConn);
			static void AllPatientsPrintOut(uhdpbs::db::connection& dbConn);
		};
	}
}
